package com.biwin.module.jwt.starter.strategy;

import com.biwin.core.api.session.IBwSessionManager;
import com.biwin.module.jwt.starter.session.BwRedisSessionManager;
import com.biwin.module.security.api.vo.BwUserDetailVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * BiWin framework Jwt Token Session Authentication Strategy
 *
 * @author biwin
 * @since 0.0.1
 */
public class BwJwtTokenAuthenticationStrategy implements SessionAuthenticationStrategy {

    private static final Log log = LogFactory.getLog(BwJwtTokenAuthenticationStrategy.class);

    private final IBwSessionManager<BwUserDetailVo> bwRedisSessionManager;

    public BwJwtTokenAuthenticationStrategy(BwRedisSessionManager<BwUserDetailVo> bwRedisSessionManager) {
        this.bwRedisSessionManager = bwRedisSessionManager;
    }

    @Override
    public void onAuthentication(Authentication authentication, HttpServletRequest request,
                                 HttpServletResponse response) throws SessionAuthenticationException {

        bwRedisSessionManager.register((BwUserDetailVo) authentication.getPrincipal());

    }
}
